package com.grass.p019lv.novel.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.androidsa.bttsddka.R;
import com.androidx.p013lv.base.bean.novel.FictionList;
import com.androidx.p013lv.base.dialog.BuyVipDialog;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.http.HttpUtils;
import com.androidx.p013lv.base.http.UrlManager;
import com.androidx.p013lv.base.http.callback.HttpCallback;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.p015ui.LazyFragment;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.grass.p019lv.bean.SearchNovelResultBean;
import com.grass.p019lv.databinding.FragmentSearchBinding;
import com.grass.p019lv.event.SearchVoiceNovelNumEvent;
import com.grass.p019lv.novel.activity.VoiceNovelActivityNew;
import com.grass.p019lv.novel.adapter.SearchVoiceNovelAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import org.greenrobot.eventbus.EventBus;

/* renamed from: com.grass.lv.novel.fragment.SearchVoiceNovelFragment */
public class SearchVoiceNovelFragment extends LazyFragment<FragmentSearchBinding> implements OnRefreshListener, OnLoadMoreListener {
    String VOICE_NOVEL_BY_TITLE = "VOICE_NOVEL_BY_TITLE";
    SearchVoiceNovelAdapter adapter;
    int page = 1;
    String searchTxt;

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public int setFragmentView() {
        return R.layout.fragment_search;
    }

    public static SearchVoiceNovelFragment newInstance(String str) {
        Bundle bundle = new Bundle();
        bundle.putString(Key.TXT, str);
        SearchVoiceNovelFragment searchVoiceNovelFragment = new SearchVoiceNovelFragment();
        searchVoiceNovelFragment.setArguments(bundle);
        return searchVoiceNovelFragment;
    }

    @Override // androidx.fragment.app.Fragment
    public void setArguments(Bundle bundle) {
        super.setArguments(bundle);
        this.searchTxt = bundle.getString(Key.TXT);
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public void initViews() {
        ((FragmentSearchBinding) this.binding).refresh.setOnRefreshListener(this);
        ((FragmentSearchBinding) this.binding).refresh.setOnLoadMoreListener(this);
        ((FragmentSearchBinding) this.binding).recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        this.adapter = new SearchVoiceNovelAdapter();
        ((FragmentSearchBinding) this.binding).recycler.setAdapter(this.adapter);
        this.adapter.setItemListener(new ItemClickListener() {
            /* class com.grass.p019lv.novel.fragment.SearchVoiceNovelFragment.C32071 */

            @Override // com.androidx.p013lv.base.listener.ItemClickListener
            public void onItemClick(View view, int i) {
                if (!SearchVoiceNovelFragment.this.isOnClick()) {
                    if (SpUtils.getInstance().getUserInfo().getFreeWatches() != -1) {
                        new BuyVipDialog(SearchVoiceNovelFragment.this.getContext()).show();
                        return;
                    }
                    int fictionId = ((FictionList) SearchVoiceNovelFragment.this.adapter.getDataInPosition(i)).getFictionId();
                    Intent intent = new Intent(SearchVoiceNovelFragment.this.getActivity(), VoiceNovelActivityNew.class);
                    intent.putExtra(Key.NOVEL_ID, fictionId);
                    SearchVoiceNovelFragment.this.getActivity().startActivity(intent);
                }
            }
        });
        ((FragmentSearchBinding) this.binding).statusLayout.setOnRetryListener(new View.OnClickListener() {
            /* class com.grass.p019lv.novel.fragment.SearchVoiceNovelFragment.View$OnClickListenerC32082 */

            public void onClick(View view) {
                SearchVoiceNovelFragment.this.page = 1;
                SearchVoiceNovelFragment.this.getInfo();
            }
        });
        getInfo();
    }

    /* access modifiers changed from: package-private */
    public void getInfo() {
        if (this.page == 1) {
            SearchVoiceNovelAdapter searchVoiceNovelAdapter = this.adapter;
            if (!(searchVoiceNovelAdapter == null || searchVoiceNovelAdapter.getData() == null || this.adapter.getData().size() <= 0)) {
                this.adapter.clear();
            }
            if (!NetUtil.isNetworkAvailable()) {
                ((FragmentSearchBinding) this.binding).statusLayout.showNoNet();
                return;
            }
            ((FragmentSearchBinding) this.binding).statusLayout.showLoading();
        }
        HttpUtils.getInsatance().get(UrlManager.getInsatance().getNovelSearchList(this.searchTxt, this.page, 2), new HttpCallback<BaseRes<SearchNovelResultBean>>(this.VOICE_NOVEL_BY_TITLE) {
            /* class com.grass.p019lv.novel.fragment.SearchVoiceNovelFragment.C32093 */

            public void onLvSuccess(BaseRes<SearchNovelResultBean> baseRes) {
                if (SearchVoiceNovelFragment.this.binding != null) {
                    ((FragmentSearchBinding) SearchVoiceNovelFragment.this.binding).statusLayout.hideLoading();
                    ((FragmentSearchBinding) SearchVoiceNovelFragment.this.binding).refresh.finishRefresh();
                    ((FragmentSearchBinding) SearchVoiceNovelFragment.this.binding).refresh.finishLoadMore();
                    if (baseRes.getCode() == 200) {
                        EventBus.getDefault().post(new SearchVoiceNovelNumEvent(baseRes.getData().getAudioTotal()));
                        if (baseRes.getData() == null || baseRes.getData().getFictionList() == null || baseRes.getData().getFictionList().size() <= 0) {
                            if (SearchVoiceNovelFragment.this.page == 1) {
                                ((FragmentSearchBinding) SearchVoiceNovelFragment.this.binding).statusLayout.showEmpty();
                            } else {
                                ((FragmentSearchBinding) SearchVoiceNovelFragment.this.binding).refresh.finishLoadMoreWithNoMoreData();
                            }
                        } else if (SearchVoiceNovelFragment.this.page == 1) {
                            SearchVoiceNovelFragment.this.adapter.setDomain(baseRes.getData().getDomain());
                            SearchVoiceNovelFragment.this.adapter.setData(baseRes.getData().getFictionList());
                            ((FragmentSearchBinding) SearchVoiceNovelFragment.this.binding).refresh.resetNoMoreData();
                        } else {
                            SearchVoiceNovelFragment.this.adapter.setDatasInEnd(baseRes.getData().getFictionList());
                        }
                    } else if (SearchVoiceNovelFragment.this.page == 1) {
                        ((FragmentSearchBinding) SearchVoiceNovelFragment.this.binding).statusLayout.showError();
                    } else {
                        ToastUtils.getInstance().showWeak(baseRes.getMsg());
                    }
                }
            }
        });
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnRefreshListener
    public void onRefresh(RefreshLayout refreshLayout) {
        this.page = 1;
        getInfo();
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        SearchVoiceNovelAdapter searchVoiceNovelAdapter = this.adapter;
        if (searchVoiceNovelAdapter == null || searchVoiceNovelAdapter.getData() == null || this.adapter.getData().size() == 0) {
            ((FragmentSearchBinding) this.binding).refresh.finishLoadMore();
            return;
        }
        this.page++;
        getInfo();
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onDestroy() {
        HttpUtils.getInsatance().cancelTag(this.VOICE_NOVEL_BY_TITLE);
        super.onDestroy();
    }
}
